//
//  NHBaseModel.m
//  LZNeiHan
//
//  Created by 梁梓烽 on 16/10/9.
//  Copyright © 2016年 liangzifeng. All rights reserved.
//

#import "NHBaseModel.h"
#import "NHFileCacheManager.h"
#import <MJExtension.h>

/** 当前类的类名 */
#define  seflClassName [self.class description]
@implementation NHBaseModel

MJCodingImplementation

+ (NSDictionary *)replacedKeyFromPropertyName {
    return @{
             @"ID":@"id",
             @"desc":@"description",
             @"responseData" : @"data"
             };
}

/** 归档- 保存模型 */
- (void)archive
{
    [NHFileCacheManager saveObject:self byFileName:seflClassName];
}


/**
 *  解档 - 取出模型
 */
- (id)unarchiver
{
 return     [NHFileCacheManager getObjectByFileName:seflClassName];
}

///**
// *  移除缓存中的模型
// */
- (void)remove
{
    [NHFileCacheManager removeObjectByFileName:seflClassName];
}
//
///**
// *  字典数组转模型数组
// */
+ (NSMutableArray *)modelArrayWithDictArray:(NSArray *)response
{
    if ([response isKindOfClass:[NSArray class]]) {
        
        return  [self mj_objectArrayWithKeyValuesArray:response];
    }
    
    return [NSMutableArray new];
}

//
///**
// *  字典转模型
// */
+ (instancetype)modelWithDictionary:(NSDictionary *)dictionary
{
    
    if ([dictionary isKindOfClass:[NSDictionary class]]) {
        
        return [self mj_objectWithKeyValues:dictionary];
    }
    return [[self alloc]init];
}

//
///**
// *  模型包含模型数组  ?  什么意思?
// */
+ (void)setUpModelClassInArrayWithContainDict:(NSDictionary *)dict
{
    
    if (dict.allKeys.count == 0) {
        return;
    }
    [self mj_setupObjectClassInArray:^NSDictionary *{
        return dict;
    }];
    
}
//
///**
// *  字典数组转模型数组  ? 什么意思?
// *  @param dict     模型包含模型数组 格式为 key-字段名字 value-[被包含的类名]
// */
+ (NSMutableArray *)modelArrayWithDictArray:(NSArray *)response containDict:(NSDictionary *)dict
{
    if (dict == nil) {
        dict = [NSMutableDictionary new];
        
    }
     [self setUpModelClassInArrayWithContainDict:dict];
    return [self modelArrayWithDictArray:response];
}
//
@end
